//
//  PayHistoryViewController.m
//  ManageMoney
//
//  Created by epwk on 15/10/14.
//  Copyright © 2015年 db. All rights reserved.
//

#import "PayHistoryViewController.h"
#import "PayHistoryCell.h"

@interface PayHistoryViewController ()

@property (strong, nonatomic) NSMutableArray *dataArray;
@property (strong, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UILabel *leftTitleLab;
@property (weak, nonatomic) IBOutlet UILabel *centerTitleLab;
@property (weak, nonatomic) IBOutlet UILabel *rightTitleLab;

@end

@implementation PayHistoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"账户查询";
    _dataArray = [NSMutableArray array];
    
    if (_chargeBool) {//充值记录
        
        
    }else
    {
        _leftTitleLab.text = @"提现金额";
        _centerTitleLab.text = @"提现手续费";
        _rightTitleLab.text = @"申请日期";
    }
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWith, 1)];
    lineView.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.3];
    [_tableView setTableFooterView:lineView];
    
//    [self setTableViewRefreshControl];
    
    [self reFreshData];
}

//加入上下拉加载控件
-(void)setTableViewRefreshControl
{
    // setup pull-to-refresh   //下拉刷新
    __weak typeof(self) weakSelf = self;
    [self.tableView addLegendHeaderWithRefreshingBlock:^{
        [weakSelf reFreshData];
    }];
    
    [self.tableView addLegendFooterWithRefreshingBlock:^{
        [weakSelf loadMoreData];
    }];
    
}
//下拉刷新 重新加载
-(void)reFreshData
{
    _offset = 0;
    [self loadTaskList];
}
//上拉  加载更多
-(void)loadMoreData
{
    _offset += kMaxLoadMore;
    [self loadTaskList];
}
//加载列表数据
-(void)loadTaskList
{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:[NSNumber numberWithInteger:_offset] forKey:@"pageSize"];
    [params setObject:[NSNumber numberWithInteger:kMaxLoadMore] forKey:@"pageNum"];
    [params setObject:_payChannelDic[@"customerNum"] forKey:@"customerNum"];
    [params setObject:_payChannelDic[@"channelNum"] forKey:@"channelNum"];
    [params setObject:_startDate forKey:@"startDate"];
    [params setObject:_endDate forKey:@"endDate"];
    
    
    if (_chargeBool) {
        
    }else
        [self requestChargeHistoryWithParams:params];
}

//ios8  后cell分割线置顶
-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}


#pragma mark - Table view data source
//分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}

//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 42;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *indentifier = @"PayHistoryCell";
    PayHistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    if (cell==nil) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"PayHistoryCell" owner:self options:nil][0];
    }
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }

    NSDictionary *dic = _dataArray[indexPath.row];
    if (_chargeBool) {
        cell.leftLabel.text = @"";
        cell.centerLable.text = @"";
        cell.rightLabel.text = @"";
    }else
    {
        cell.leftLabel.text = [NSString stringWithFormat:@"%@元",dic[@"TXJE"]];
        cell.centerLable.text = [NSString stringWithFormat:@"%@",dic[@"TXSXF"]];
        cell.rightLabel.text = [NSString stringWithFormat:@"%@",dic[@"SQRQ"]];
    }

    
    return cell;
    
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    

}

#pragma mark - RequestAction 充值渠道
-(void)requestChargeHistoryWithParams:(NSDictionary *)pramas
{
    [SVProgressHUD show];
    [[AFNetApiClient sharedClient] requestChargeHistoryWithParams:pramas success:^(AFHTTPRequestOperation *operation , id responseObject) {
        [SVProgressHUD dismiss];
        NSDictionary *dic = responseObject;
        if ([dic[@"responseCode"] intValue] == 200) {
            NSArray *ary = dic[@"responseObj"];
            if (_offset == 0) {
                [_dataArray removeAllObjects];
                [_dataArray addObjectsFromArray:ary];
            }else
            {
                [_dataArray addObjectsFromArray:ary];
            }
            
            [_tableView reloadData];
            
            if (ary.count < kMaxLoadMore) {
                //没有更多数据  隐藏加载更多的功能
                self.tableView.footer.hidden = YES;
            }else
            {
                self.tableView.footer.hidden = NO;
            }
            
            
            [self.tableView.header endRefreshing];
            [self.tableView.footer endRefreshing];
        }else
        {
            [SVProgressHUD showErrorWithStatus:dic[@"responseMessage"]];
        }
    } failure:^(AFHTTPRequestOperation *operation , NSError *error) {
        NSLog(@"%@",error);
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
        if (_offset > 0) {
            _offset-=kMaxLoadMore;
        }

        [SVProgressHUD dismiss];
    }];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
